<?php

namespace apps\common\controllers;
if( ! APP_ROOT  ) return 'Direct Access Deny!';

use Phalcon\Mvc\Controller;
use enums\GrepEnums;
use libraries\CommUtils;

use enums\RetCodeEnums;


return;

/**
 * 由于性能等原因暂不对外提供服务
 * 
 * @author fzq
 * @date 2016-12-08
 */
class JuheController extends Controller
{
	use JsonRet;
	
	/**
	 * 聚合数据银行卡三元素查询
	 * 
	 * 使用方法
	 * 
	 * http://c.bwyt.dev/common/juhe/bankcardcheckthreeelem/realname/%20%E9%92%B1%E5%BF%97%E9%94%8B%20/idcard/412726198503156258/bankcard/6216603600001256555
	 * 
	 * {"code":0,"msg":"\u6210\u529f"}
	 */
	public function bankCardCheckThreeElemAction()
	{
		$arrParams = $this->dispatcher->getParams();
		if( !$arrParams || empty( $arrParams ))
		{
			echo $this->getRet(
					null,
					RetCodeEnums::ERR_JUHE_BANK3_IDCARD_MSG,
					RetCodeEnums::ERR_JUHE_BANK3_IDCARD );//error
				
			return;
		}
		
		//身份证号
		$strIDCard = $this->dispatcher->getParam( 'idcard', 'trim' );
		if( !$strIDCard || false === CommUtils::validationFilterIDCard( $strIDCard ))
		{
			echo $this->getRet( 
					null, 
					RetCodeEnums::ERR_JUHE_BANK3_IDCARD_MSG, 
					RetCodeEnums::ERR_JUHE_BANK3_IDCARD );//error
			
			return;
		}
		
		//银行卡号
		$strBankCard = $this->dispatcher->getParam( 'bankcard', 'trim' );
		if( !$strBankCard || !preg_match( GrepEnums::GREP_BANK, $strBankCard ) )
		{
			echo $this->getRet( 
								null, 
								RetCodeEnums::ERR_JUHE_BANK3_BANKCARD_MSG, 
								RetCodeEnums::ERR_JUHE_BANK3_BANKCARD );//error
			
			return;
		}
		
		//姓名
		$strRealName = $this->dispatcher->getParam( 'realname', 'trim' );
		if( !$strRealName )
		{
			echo $this->getRet(
					null,
					RetCodeEnums::ERR_JUHE_BANK3_NAME_MSG,
					RetCodeEnums::ERR_JUHE_BANK3_NAME );//error
			
			return;
		}
		
		$strUrl = $this->di[ 'juheCfg' ]->bank3e->url . 
					'key=' . $this->di[ 'juheCfg' ]->bank3e->key . 
					'&realname=' . $strRealName . 
					'&idcard=' . $strIDCard . 
					'&bankcard=' . $strBankCard;
		
		$redis = $this->di['nredis'];
		if( $cacheData = $redis->hGet( $this->di[ 'juheCfg' ]->bank3e->cacheName, $strBankCard ) )
		{
			$objBankInfo = json_decode( $cacheData );
			
			if( $objBankInfo->realname == urldecode( $strRealName ) &&
				$objBankInfo->idcard == $strIDCard && 
				$objBankInfo->bankcard == $strBankCard
			)
			{
				echo $this->getRet( null, RetCodeEnums::ERR_SUCCESS_MSG, RetCodeEnums::ERR_SUCCESS );
			}
			else
			{
				$this->di['errLog']->error( RetCodeEnums::ERR_JUHE_BANK3_VERIFY . ' ' . 
						RetCodeEnums::ERR_JUHE_BANK3_VERIFY_MSG  . ' ' . var_export( $objBankInfo, true ) );
				
				echo $this->getRet( null, RetCodeEnums::ERR_JUHE_BANK3_VERIFY_MSG, RetCodeEnums::ERR_JUHE_BANK3_VERIFY );
			}
			
			return;
		}
		
		$objBankInfo = $this->getResult( $strUrl, true );
		if( $objBankInfo->error_code  == 0 )
		{//查询成功
			if( $objBankInfo->result->res == 1 )
			{//验证成功
				echo $this->getRet( null, RetCodeEnums::ERR_SUCCESS_MSG, RetCodeEnums::ERR_SUCCESS );
				
				$arrBankData = array( 
						'bankcard' => $objBankInfo->result->bankcard, 
						'realname' => $objBankInfo->result->realname, 
						'idcard' => $objBankInfo->result->idcard );
				
				
				$redis->hSet( $this->di[ 'juheCfg' ]->bank3e->cacheName, $strBankCard, json_encode( $arrBankData ) );
				return;
			}
			else
			{
				$this->di['errLog']->error( RetCodeEnums::ERR_JUHE_BANK3_VERIFY . ' ' . 
						RetCodeEnums::ERR_JUHE_BANK3_VERIFY_MSG  . ' ' . var_export( $objBankInfo, true ) );
				
				echo $this->getRet( null, RetCodeEnums::ERR_JUHE_BANK3_VERIFY_MSG, RetCodeEnums::ERR_JUHE_BANK3_VERIFY );
			}
		}
		else
		{//验证失败
			$this->di['errLog']->error( RetCodeEnums::ERR_OTHERS . ' ' . RetCodeEnums::ERR_OTHERS_MSG . ' ' . var_export( $objBankInfo, true ) );
			
			echo $this->getRet( null, RetCodeEnums::ERR_OTHERS_MSG, RetCodeEnums::ERR_OTHERS );
		}
	}
	
	
	private function getResult( $strUrl, $bDecode = false )
	{
		try
		{
			$curl = curl_init( $strUrl );
			curl_setopt( $curl, CURLOPT_TIMEOUT, 10 );
			curl_setopt( $curl, CURLOPT_HEADER, 0 );
			curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
			$strRet = curl_exec( $curl );
			curl_close( $curl );
		}
		catch ( \Exception $e )
		{
			$this->di['errLog']->debug( $e->getMessage() );
			
			return false;
		}
	
		if( $bDecode )
		{
			return json_decode( $strRet );
		}
		else
		{
			return $strRet;
		}
	}
}